package com.jt.tenant.conn;

import com.jt.tenant.app.JTApplication;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyType;
import com.zcx.helper.http.note.HttpCache;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/9.
 */
@HttpInlet(Conn.INLET_GET_TRAVE_DETAIL)
@HttpCache(name = AsyType.MAX_STALE, time = 86400)
public class GetTraveDetail extends JTAsyGet<GetTraveDetail.Info>{

    public String token = JTApplication.jtPreferences.readToken();

    public String obdid = JTApplication.jtPreferences.readOdbID();

    public String travelid;

    public GetTraveDetail(AsyCallBack<Info> asyCallBack) {

        super(asyCallBack);

    }

    @Override
    protected Info parserBody(Object body) {

        JSONObject object = ((JSONObject)body).optJSONObject("travel");

        Info info = new Info();

        if(object != null){

            info.startTime = object.optLong("startTime");

            info.startName = object.optString("startName");

            info.speedUp = object.optString("speedUp");

            info.avgspeed = object.optString("avgspeed");

            info.mileage = object.optDouble("mileage");

            info.speedDown = object.optString("speedDown");

            info.score = object.optString("score");

            info.fuel = object.optString("fuel");

            info.endName = object.optString("endName");

            info.endTime = object.optLong("endTime");

            info.sh = object.optString("sh");

        }

        JSONArray array = ((JSONObject)body).optJSONArray("gpsList");

        if(array != null){

            for (int i = 0 ; i < array.length() ; i++){

                JSONObject o = array.optJSONObject(i);

                Gps gps = new Gps();

                gps.LATITUDE = o.optDouble("LATITUDE");

                gps.LONGITUDE = o.optDouble("LONGITUDE");

                if(!(gps.LATITUDE == 0) && !(gps.LONGITUDE == 0)){

                    info.gpsList.add(gps);

                }

            }

        }

        return info;
    }

    public static class Info{

        public long startTime;

        public String startName;

        public String speedUp;

        public String avgspeed;

        public double mileage;

        public String speedDown;

        public String score;

        public String fuel;

        public String endName;

        public String sh;

        public long endTime;

        public List<Gps> gpsList = new ArrayList<>();

    }

    public static class Gps{

        public double LONGITUDE, LATITUDE;

    }

}
